package com.esms.struts.service;

public final class Strings {
	public static boolean isBlank(String str) {
		return (str == null || str.length() == 0 || str.trim().length() == 0);
	}
	
	/**
	 * hi all , we should care &lt;, &gt;, &, ", ' and null 
	 * when you display those characters in the UI page
	 * 
	 * @author yangjian
	 */
	public static String encodeHtml(Object value) {
		if (value == null) {
			return "";
		}
		return encodeHtml(value.toString());
	}
	
	public static String encodeHtml(String value) {
		if (value == null) {
			return "";
		}

		if (value.indexOf('<') < 0 && value.indexOf('>') < 0 && value.indexOf('&') < 0
				&& value.indexOf('"') < 0 && value.indexOf('\'') < 0) {
			return value;
		}

		char content[] = new char[value.length()];
		value.getChars(0, value.length(), content, 0);
		StringBuffer result = new StringBuffer(content.length + 50);
		for (int i = 0; i < content.length; i++) {
			switch (content[i]) {
			case '<':
				result.append("&lt;");
				break;
			case '>':
				result.append("&gt;");
				break;
			case '&':
				result.append("&amp;");
				break;
			case '"':
				result.append("&quot;");
				break;
			default:
				result.append(content[i]);
			}
		}
		return result.toString();
	}
}